package com.tmsps.scan.util;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import com.tmsps.scan.exception.MenuScannerException;



public class FileUtil {
	

	/**
	 * 获取文件文本内容
	 * @param fileUrl 文件路径
	 * @return
	 * @throws Exception 
	 */
	public static String getFileText(String fileUrl) {
		fileUrl = removeBackSlant(fileUrl);	//去除url结尾的所以反斜杠
		if(fileUrl==null || fileUrl.length()==0){
			String message = "文件路径为空!!";
			throw new RuntimeException(message);
		}
		
		File file = new File(fileUrl);
		
		String fileContentString = null;
		if(!file.isFile()){
			throw new MenuScannerException("不是一个标准的文件");
		}
		
		FileInputStream is = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			String charset = EncodingDetect.detect(file);
			is = new FileInputStream(file);
			byte[] buffers = new byte[512];
			int count = 0;
			while((count = is.read(buffers))>0){
				bos.write(buffers, 0, count);
			}
			fileContentString = new String(bos.toByteArray(),charset);
		} catch (Exception e) {
			throw new MenuScannerException(e);
		} finally {
			if(is != null){
				try {
					is.close();
					is = null;
				} catch (IOException e) {
					throw new MenuScannerException(e);
				}
			}
			if(bos != null){
				try {
					bos.close();
					bos = null;
				} catch (IOException e) {
					throw new MenuScannerException(e);
				}
			}
		}
		
		return fileContentString;
	}
	
	/**
	 * 创建文本文件
	 * @param text
	 * @param catalogUrl
	 */
	public static void text2File(String text,String catalogUrl){
		catalogUrl = removeBackSlant(catalogUrl);	//去除url结尾的所以反斜杠
		if(catalogUrl==null || catalogUrl.length()==0){
			String message = "文件路径为空!!";
			throw new RuntimeException(message);
		}
		File file = new File(catalogUrl);
		if(!file.exists()){
			try {
				file.createNewFile();
				if(!file.exists()){
					throw new MenuScannerException("不是一个标准的文件");
				}
			} catch (IOException e) {
				throw new MenuScannerException("创建文件失败!",e);
			}
		}
		OutputStreamWriter writer = null;
		BufferedWriter out = null;
		try {
			String charsetName = EncodingDetect.detect(file);
			writer = new OutputStreamWriter(new FileOutputStream(file), charsetName);
			out = new BufferedWriter(writer);
			out.write(text, 0, text.length());
			out.close();
		} catch (IOException e) {
			throw new MenuScannerException(e);
		} finally {
			if(writer != null){
				try {
					writer.close();
					writer = null;
				} catch (IOException e) {
					throw new MenuScannerException(e);
				}
			}
			if(out != null){
				try {
					out.close();
					out = null;
				} catch (IOException e) {
					throw new MenuScannerException(e);
				}
			}
		}
	}
	
	/**
	 * 对文件文本内容选择替换
	 * @param fileUrl 文件路径
	 * @param oldChar 要替换的内容数组
	 * @param newChar 替换的新内容数组
	 */
	public static void replaceFileText(String fileUrl, String[] oldChars, String[] newChars) {
		fileUrl = removeBackSlant(fileUrl);	//去除url结尾的所以反斜杠
		if(fileUrl==null || fileUrl.length()==0){
			String message = "文件路径为空!!";
			throw new RuntimeException(message);
		}
		if(oldChars==null){
			String message = "要替换的内容数组为null";
			throw new RuntimeException(message);
		}
		if(newChars==null){
			String message = "替换的新内容数组为null";
			throw new RuntimeException(message);
		}
		if(oldChars.length!=newChars.length){
			String message = "旧内容数组与新内容数组长度不一致";
			throw new RuntimeException(message);
		}
		String fileContentString = getFileText(fileUrl);	//得到文件文本内容
		fileContentString = fileContentString.replaceAll("(\r\n)|(\n)", "\\\\r\\\\n");
		for(int i=0;i<oldChars.length;i++){
			String oldChar = oldChars[i];
			String newChar = newChars[i];
			fileContentString = fileContentString.replace(oldChar, newChar);
		}
		fileContentString = fileContentString.replaceAll("(\\\\r\\\\n)|(\\\\n)", "\r\n");
		text2File(fileContentString,fileUrl);
	}
	
	/**
	 * 提取文本文件内容并可替换内容后到新文件
	 * @param fileUrl
	 * @param catalogUrl
	 * @param oldChars
	 * @param newChars
	 */
	public static void fileText2NewFile(String fileUrl,String catalogUrl , String[] oldChars, String[] newChars){
		fileUrl   =  removeBackSlant(fileUrl);	//去除url结尾的所以反斜杠
		catalogUrl = removeBackSlant(catalogUrl);
		if(fileUrl==null || fileUrl.length()==0){
			String message = "文件路径为空!!";
			throw new RuntimeException(message);
		}
		if(catalogUrl==null || catalogUrl.length()==0){
			String message = "文件路径为空!!";
			throw new RuntimeException(message);
		}
		if(oldChars==null){
			String message = "要替换的内容数组为null";
			throw new RuntimeException(message);
		}
		if(newChars==null){
			String message = "替换的新内容数组为null";
			throw new RuntimeException(message);
		}
		if(oldChars.length!=newChars.length){
			String message = "旧内容数组与新内容数组长度不一致";
			throw new RuntimeException(message);
		}
		String fileContentString = getFileText(fileUrl);	//得到文件文本内容
		for(int i=0;i<oldChars.length;i++){
			String oldChar = oldChars[i];
			String newChar = newChars[i];
			fileContentString = fileContentString.replace(oldChar, newChar);
		}
		text2File(fileContentString,catalogUrl);
	}
	
	/**
	 * 去除字符串后的反斜杠"\"
	 * 会将正斜杠换为两个反斜杠,并且去除结尾的反斜杠"\"
	 * @param url
	 * @return
	 */
	public static String removeBackSlant(String url){
		String _url = url.replaceAll("/", File.separator);
		boolean endsWith = _url.endsWith(File.separator);
		if(endsWith){
			int lastIndexOf = _url.lastIndexOf(File.separator);
			String _new = _url.substring(0, lastIndexOf);
			return removeBackSlant(_new);
		} else {
			return _url;
		}
	}
}
